package com.puhui.lib.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Process;

import java.util.List;

public class AppInfoUtils {
    /**
     * 判断该进程是否是app进程
     */
    public boolean isAppProcess(Context context) {
        String processName = getProcessName(context);
        if (processName == null || !processName.equalsIgnoreCase(context.getPackageName())) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 获取运行该方法的进程的进程名
     */
    public static String getProcessName(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (null != am) {
            List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
            if (runningApps == null) {
                return null;
            }
            for (ActivityManager.RunningAppProcessInfo procInfo : runningApps) {
                if (procInfo.pid == Process.myPid()) {
                    return procInfo.processName;
                }
            }
        }
        return null;
    }
}
